package com.ruoyi.web.controller.app;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.ResponseResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Withdraw;
import com.ruoyi.system.dto.AddDto;
import com.ruoyi.system.dto.WithdrawSubmitDto;
import com.ruoyi.system.service.WithdrawService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Map;

@Api(tags = "用户端:收支记录")
@RestController
@RequestMapping("app/money")
public class WithdrawController extends AppController {
    @Autowired
    private WithdrawService withdrawService;

    @Log(title = "提现", businessType = BusinessType.INSERT)
    @ApiOperation(value = "提现")
    @PostMapping("withdraw")
    public ResponseResult withdraw(@Valid @RequestBody WithdrawSubmitDto withdrawSubmitDto) {
        SysUser user = getSysUser();
        withdrawService.submitWithdrawal(user, withdrawSubmitDto.getMoney(), withdrawSubmitDto.getAddress());
        return this.setSuccessResult();
    }

    @ApiOperation(value = "提现列表")
    @GetMapping("withdrawList")
    public ResponseResult<List<Withdraw>> withdrawList(String status) {
        String type = "withdraw";
        List<Withdraw> list = this.list(type, status);
        return this.setSuccessResult(list);
    }
    @Log(title = "充值", businessType = BusinessType.INSERT)
    @ApiOperation(value = "充值")
    @PostMapping("recharge")
    public ResponseResult<Long> submitRecharge(@Valid @RequestBody WithdrawSubmitDto withdrawSubmitDto) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long id = withdrawService.submitRecharge(user, withdrawSubmitDto.getMoney(), withdrawSubmitDto.getAddress());
        return this.setSuccessResult(id);
    }

    @ApiOperation(value = "详情")
    @GetMapping("getById")
    public ResponseResult getById(Long id) {
        Withdraw withdraw = withdrawService.getById(id);
        if (withdraw == null) {
            return this.setErrorResult("数据不存在");
        }

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        QrCodeUtil.generate(withdraw.getCashAddress(), new QrConfig(), ImgUtil.IMAGE_TYPE_PNG, outputStream);
        byte[] pngData = outputStream.toByteArray();
        String base64 = "data:image/png;base64," + Base64.encode(pngData);
        Map<String, Object> map = BeanUtil.beanToMap(withdraw);
        map.put("cashAddressBase64", base64);
        return this.setSuccessResult(map);
    }

    public static void main(String[] args) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        QrCodeUtil.generate("TLYC5TYMjo8kBAG2KTDCmRtjEGbFJy22LU", new QrConfig(), ImgUtil.IMAGE_TYPE_PNG, outputStream);
        byte[] pngData = outputStream.toByteArray();
        System.out.println("data:image/png;base64," + Base64.encode(pngData));
        ;
    }

    @ApiOperation(value = "取消充值")
    @PostMapping("cancel")
    public ResponseResult<Long> cancel(@RequestBody AddDto addDto) {

        Withdraw withdraw = withdrawService.getById(addDto.getId());
        if (!StrUtil.equals("init", withdraw.getStatus())) {
            return this.setErrorResult("状态有误");
        }
        withdraw.setStatus("cancel");
        withdrawService.updateDomain(withdraw);
        return this.setSuccessResult(withdraw);
    }

    @ApiOperation(value = "我已汇款")
    @PostMapping("toReviewed")
    public ResponseResult<Long> toReviewed(@RequestBody AddDto addDto) {

        Withdraw withdraw = withdrawService.getById(addDto.getId());
        if (!StrUtil.equals("init", withdraw.getStatus())) {
            return this.setErrorResult("状态有误");
        }
        withdraw.setStatus("reviewed");
        withdrawService.updateDomain(withdraw);
        return this.setSuccessResult(withdraw);
    }


    @ApiOperation(value = "充值列表")
    @GetMapping("rechargeList")
    public ResponseResult<List<Withdraw>> rechargeList(String status) {
        String type = "recharge";
        List<Withdraw> list = this.list(type, status);
        return this.setSuccessResult(list);
    }

    private List<Withdraw> list(String type, String status) {
        Long userId = this.getUserId();
        this.startPage();
        Withdraw withdraw = new Withdraw();
        withdraw.setUserId(userId);
        withdraw.setType(type);
        if (StrUtil.isNotBlank(status)) {
            withdraw.setStatus(status);
        }
        List<Withdraw> list = withdrawService.findList(withdraw);
        return list;
    }


}
